//
//  ChargeViewController.m
//  EasyToo
//
//  Created by linshengqin on 13-8-29.
//
//

#import "ChargeViewController.h"
#import "DeviceUtil.h"
#import "GameManager.h"
#import "GameModel.h"
#import "GameInfo.h"
#import "DeviceUtil.h"

#define SCREEN_HEIGHT [DeviceUtil getScreenHeight]
@interface ChargeViewController ()

@end

@implementation ChargeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}



- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    game_model = [GameModel shareData];

 //   [self loadGameData];
    [self.navigationItem setTitle:@"充值"];
    
    [self initMobileView];
    [self initGameView];
    [self initQQView];
    
    [self initSegmentCtrl];
    
    [self addMobileView];

}

- (void)dealloc
{
    [_mobileView release];
    [_gameView release];
    [_qqView release];
    [super dealloc];
}

- (void)initSegmentCtrl
{
    UISegmentedControl* segment = [[UISegmentedControl alloc] initWithFrame:CGRectMake(0, 0+ADD_ADJUST_IOS7STATUES_HIGHT-20, [DeviceUtil getScreenWidth], 30)];
    
    [segment setSegmentedControlStyle:UISegmentedControlStyleBar];
    [segment setBackgroundColor:[UIColor lightGrayColor]];
    [segment insertSegmentWithTitle:@"手机充值" atIndex:0 animated:NO];
    [segment insertSegmentWithTitle:@"游戏充值" atIndex:1 animated:NO];
    [segment insertSegmentWithTitle:@"Q币" atIndex:2 animated:NO];
    
    [segment addTarget:self action:@selector(onTabSelected:) forControlEvents:UIControlEventValueChanged];
    
    [self.view addSubview:segment];
}

- (void)initMobileView
{
    _mobileView =  [[MobilephoneChargeView alloc] initWithFrame:CGRectMake(0, 30+ADD_ADJUST_IOS7STATUES_HIGHT-20, [DeviceUtil getScreenWidth], SCREEN_HEIGHT - 30)];
    _mobileView.fatherView = self;
}

- (void)initGameView
{
    _gameView = [[GameChargeView alloc] initWithFrame:CGRectMake(0, 30+ADD_ADJUST_IOS7STATUES_HIGHT-20, [DeviceUtil getScreenWidth], SCREEN_HEIGHT - 30)];
    _gameView.fatherView = self;
}

- (void)initQQView
{
    _qqView = [[QQChargeView alloc] initWithFrame:CGRectMake(0, 30+ADD_ADJUST_IOS7STATUES_HIGHT-20, [DeviceUtil getScreenWidth], SCREEN_HEIGHT - 30)];
    _qqView.fatherView = self;
}

- (void)addMobileView
{
    if (_mobileView)
    {
        [self.view addSubview:_mobileView];
    }
}

- (void)addGameView
{
    if (_gameView) {
        [self.view addSubview:_gameView];
    }
}

- (void)addQQView
{
    if (_qqView) {
        [self.view addSubview:_qqView];
    }
}

- (void)removeMobileView
{
    if (_mobileView)
    {
        [_mobileView removeFromSuperview];
    }
}

- (void)removeGameView
{
    if (_gameView)
    {
        [_gameView removeFromSuperview];
    }
}

- (void)removeQQView
{
    if (_qqView) {
        [_qqView removeFromSuperview];
    }
}


-(void)onTabSelected:(id)sender
{
    UISegmentedControl *control = (UISegmentedControl *)sender;
    
    switch (control.selectedSegmentIndex) {
        case 0:
            [self addMobileView];
            [self removeGameView];
            [self removeQQView];
            break;
        case 1:
            [self addGameView];
            [self removeMobileView];
            [self removeQQView];
            break;
        case 2:
            [self addQQView];
            [self removeMobileView];
            [self removeGameView];
            break;
        default:
            break;
    }
    
}

-(void) loadGameData
{
    [GameManager shareManager].delegate = self;
    [[GameManager shareManager] searchAllGame];
    //NSArray *array = [NSArray arrayWithObjects:@"1",@"5",@"10",@"15",@"30",@"50",@"100", nil];
    //[game_model.priceArray setArray:array];

}

- (void)onSearchAllGameFinish:(BOOL)isSuccess
                        Games:(NSMutableArray*)gameInfoArray
{
    if(isSuccess)
    {
        printf("%s,%d\n",__func__,__LINE__);

        
        for(GameInfo *obj in gameInfoArray)
        {
            NSLog(@"gameid is:%@,gamename is:%@\n",obj.gameId,obj.gameName);
            [game_model.gameArray addObject:(obj.gameName)];
            [game_model.gameIdArray addObject:(obj.gameId)];
        }
        NSLog(@"datamodel is:%@",game_model.gameArray);
        printf("pickerarray count is:%d",[game_model.gameArray count]);

 //   game_model.pickerArray = [[NSArray arrayWithArray:gameInfoArray] retain];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
